﻿using Dapper;
using SassModule.Contracts;
using SassModule.Entity;
using SassModule.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SassModule.Services
{
    /// <summary>
    /// 角色服务
    /// </summary>
    public class RoleService : IRoleService
    {
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="info"></param>
        /// <param name="dbStr"></param>
        /// <returns></returns>
        public string Add(Role info, string dbStr = null)
        {
            using var db = SassModule.ConnCache.GetConn();
            //获取账套Id
            var sasses = db.Query<Sass>("SELECT * FROM Sys_Sass WHERE DB=@dbStr", new { dbStr }).ToList();
            if (sasses.Count <= 0) return "账套不存在";
            info.SassId= sasses[0].Id;
            db.Execute("INSERT INTO Sys_Role(Id,Name,CreateTime,Description,SassId) VALUES(NEWID(),@Name,GETDATE(),@Description,@SassId)", info);
            return null;
        }

        /// <summary>
        /// 根据用户Id删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dbStr"></param>
        public void Del(Guid id, string dbStr = null)
        {
            using var db = SassModule.ConnCache.GetConn();
            db.Execute("DELETE FROM Sys_Role WHERE Id=@id", new { id});
        }
        /// <summary>
        /// 获取角色下的菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dbStr"></param>
        /// <returns></returns>
        public IEnumerable<string> GetMenus(Guid id, string dbStr = null)
        {
            using var db = SassModule.ConnCache.GetConn();
            return db.Query<string>("SELECT DISTINCT Path FROM Sys_Menu M LEFT JOIN Sys_RoleMenu RM ON M.Id = RM.MId WHERE RM.RId = @id",new { id});
        }

        /// <summary>
        /// 获取该账套下所有的角色
        /// </summary>
        /// <param name="dbStr"></param>
        /// <returns></returns>
        public IEnumerable<Role> Index(string dbStr = null)
        {
            using var db = SassModule.ConnCache.GetConn();
            //获取账套Id
            var sasses = db.Query<Sass>("SELECT * FROM Sys_Sass WHERE DB=@dbStr", new { dbStr }).ToList();
            if (sasses.Count <= 0) return null;
            return db.Query<Role>("SELECT * FROM Sys_Role WHERE SassId=@sassId", new { sassId=sasses[0].Id});
        }
        /// <summary>
        /// 根据Id获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dbStr"></param>
        /// <returns></returns>
        public Role Index(Guid id, string dbStr = null)
        {
            using var db = SassModule.ConnCache.GetConn();
            return db.QueryFirstOrDefault<Role>("SELECT * FROM Sys_Role WHERE Id=@id", new { id});
        }
        /// <summary>
        /// 为角色设置菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="menus"></param>
        /// <param name="dbStr"></param>
        public void SetMenus(Guid id, List<string> menus, string dbStr = null)
        {
            using var db = SassModule.ConnCache.GetConn();
            db.Execute("DELETE FROM Sys_RoleMenu WHERE RId=@id", new { id });
            //获取所有的菜单Id
            var ms = db.Query<Guid>("SELECT Id FROM Sys_Menu WHERE Path IN @menus", new { menus });
            foreach (var item in ms)
            {
                db.Execute("INSERT INTO Sys_RoleMenu(RId,MId) VALUES(@rid,@mid)", new { rid = id, mid = item });
            }
        }

        public void UpdateRoles(Role info)
        {
            if (info.Id == null) return;
            //写sql语句
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("UPDATE  Sys_Role SET");
            if (info.Name !=null)
            {
                sql.AppendLine("[Name] = @Name,");
            }
            if (info.Status!=null)
            {
                sql.AppendLine("[Status] = @Status,");
            }
            if (info.Description!=null)
            {
                sql.AppendLine("[Description] = @Description,");
            }
            sql.AppendLine("[SassId] = @SassId");
            sql.AppendLine("WHERE [id]=@id");
            using var db = SassModule.ConnCache.GetConn();
            db.Execute(sql.ToString(),info);
        }
    }
}
